
//
//  ShowMyIconViewController.swift
//  DiDiRecycle
//
//  Created by abb on 16/8/2.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SVProgressHUD
class ShowMyIconViewController: AbbBaseViewController {
    
    var image:UIImage? {
        didSet {
            imageView.image = image
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // 背景色
        view.backgroundColor = UIColor.blackColor()
        // 控件
        view.addSubview(imageView)
        imageView.snp_makeConstraints { (make) in
            make.center.equalTo(view)
            make.left.right.equalTo(view)
            make.height.equalTo(imageView.snp_width)
        }
        // 右上角item
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigationbar_more"), style: .Plain, target: self, action: #selector(changeIcon))
        SVProgressHUD.setMinimumDismissTimeInterval(1)
    }
    /**
     弹出UIAlertController
     */
    func changeIcon() {
        self.presentViewController(userIconAlert, animated: true, completion: nil)
    }
    
    func uploadIcon(icon:UIImage) {
        SVProgressHUD.showWithStatus("头像上传中")
        UserDAO.setIcon(icon) { (dict, error) in
            if error == nil {
                if let user = User.loadUser() {
                    user.userImageUrlStr = dict!["user_image_url"] as? String
                    user.userImageUrlHdStr = dict!["user_image_url_hd"] as? String
                    User.updataUser(user)
                }
                SVProgressHUD.showSuccessWithStatus("上传成功")
            } else {
                SVProgressHUD.showErrorWithStatus(error)
            }
        }
    }

    
    // MARK: - 懒加载
    private lazy var imageView:UIImageView = UIImageView()
    

    private lazy var  userIconAlert:UIAlertController =  {
        let alertAction = UIAlertController(title: "请选择操作", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
        // 从相册
        let chooseFromPhotoAlbum = UIAlertAction(title: "从相册选择", style: UIAlertActionStyle.Default, handler: { (alertAction) in
            //设置图片源
            self.imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            //模态弹出IamgePickerView
            self.presentViewController(self.imagePicker, animated: true, completion: nil)
        })
        // 从相机
        let chooseFromCamera = UIAlertAction(title: "拍照", style: UIAlertActionStyle.Default,handler: { (alertAction) in
            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
                //设置图片源
                self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
                //模态弹出IamgePickerView
                self.presentViewController(self.imagePicker, animated: true, completion: nil)
            }
        })
        // 取消
        let canelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel,handler: nil)
        alertAction.addAction(chooseFromCamera)
        alertAction.addAction(chooseFromPhotoAlbum)
        alertAction.addAction(canelAction)
        return alertAction
    }()
    
    private lazy var imagePicker:UIImagePickerController = {
        let imagePicker = UIImagePickerController()
        //设置代理
        imagePicker.delegate = self
        //允许编辑
        imagePicker.allowsEditing = true
        return imagePicker
    }()
    
    

}
extension ShowMyIconViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //获得编辑后的图片
        let getImage = info[UIImagePickerControllerEditedImage] as! UIImage
        self.image = getImage
        picker.dismissViewControllerAnimated(true, completion: nil)
        uploadIcon(getImage)
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}